// create-ssl.js
require('dotenv').config({
    path: "/root/cloudflare.ini"
});
const axios = require('axios');

const CLOUDFLARE_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN = process.env.dns_cloudflare_api_key;
const ZONE_ID = process.env.CLOUDFLARE_ZONE_ID;

// 创建 SSL 证书
async function createSSLCertificate() {
  try {
    const response = await axios.post(
      `https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/ssl/certificate_packs`,
      {
        type: 'advanced',
        hosts: ['deeplove.site', '*.deeplove.site'], // 需要申请证书的域名
        validation_method: 'txt', // 采用 DNS TXT 记录验证
      },
      {
        headers: {
          Authorization: `Bearer ${CLOUDFLARE_API_TOKEN}`,
          'Content-Type': 'application/json',
        },
      }
    );

    if (response.data.success) {
      console.log('✅ SSL 证书申请成功:', JSON.stringify(response.data.result, null, 2));
    } else {
      console.error('❌ SSL 证书申请失败:', response.data.errors);
    }
  } catch (error) {
    console.error('❌ 请求错误:', error.response?.data || error.message);
  }
}

createSSLCertificate();